package org.gdstash.character;

import org.gdstash.file.GDReader;
import org.gdstash.file.GDWriter;
import org.gdstash.ui.character.GDCharFactionPane;
import org.gdstash.util.FileVersionException;
import org.gdstash.util.GDMsgFormatter;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class GDCharFactionList {
    private static final int VERSION = 5;
    private static final int BLOCK = 13;
    private int version;
    private List<GDCharFaction> factions;
    private int faction;
    protected boolean changed;

    public GDCharFactionList() {
        this.factions = new LinkedList<GDCharFaction>();
        this.changed = false;
    }

    public int[] getFactionReputations() {
        final int[] reputations = new int[GDCharFactionPane.FACTIONS.length];
        int pos = 0;
        for (final GDCharFaction faction : this.factions) {
            for (int i = 0; i < GDCharFactionPane.FACTIONS.length; ++i) {
                if (GDCharFactionPane.FACTIONS[i] == pos) {
                    reputations[i] = faction.getFactionReputation();
                    break;
                }
            }
            ++pos;
        }
        return reputations;
    }

    public boolean hasChanged() {
        return this.changed;
    }

    public void setFactionReputations(final int[] reputations) {
        int pos = 0;
        for (final GDCharFaction faction : this.factions) {
            for (int i = 0; i < GDCharFactionPane.FACTIONS.length; ++i) {
                if (GDCharFactionPane.FACTIONS[i] == pos) {
                    faction.setFactionReputation(reputations[i]);
                    break;
                }
            }
            ++pos;
        }
        this.changed = true;
    }

    public void read() throws IOException {
        int val = 0;
        final GDReader.Block block = new GDReader.Block();
        val = GDReader.readBlockStart(block);
        if (val != 13) {
            throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
        }
        this.version = GDReader.readEncInt(true);
        if (this.version != 5) {
            throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
        }
        this.faction = GDReader.readEncInt(true);
        this.factions.clear();
        val = GDReader.readEncInt(true);
        for (int i = 0; i < val; ++i) {
            final GDCharFaction f = new GDCharFaction();
            f.read();
            this.factions.add(f);
        }
        GDReader.readBlockEnd(block);
        this.changed = false;
    }

    public void write() throws IOException {
        final GDReader.Block block = new GDReader.Block();
        GDWriter.writeBlockStart(block, 13);
        GDWriter.writeInt(5);
        GDWriter.writeInt(this.faction);
        final int val = this.factions.size();
        GDWriter.writeInt(val);
        for (final GDCharFaction faction : this.factions) {
            if (faction != null) {
                faction.write();
            }
        }
        GDWriter.writeBlockEnd(block);
        this.changed = false;
    }

    public int getByteSize() {
        int size = 0;
        size += 4;
        size += 4;
        size += 4;
        size += 4;
        size += 4;
        size += this.factions.size() * GDCharFaction.getByteSize();
        size += 4;
        return size;
    }
}
